#include "Render/CubeRenderable.h"

#include <glut.h>

namespace vital {

TCubeRenderable::TCubeRenderable(float halfSize) {

	buildCubeMesh(halfSize);
}

TCubeRenderable::~TCubeRenderable() {
}


void TCubeRenderable::render() {

	glBegin(GL_TRIANGLES);

	//  0   3
	//  1   2
	// 4   7
	// 5   6

	// +x
	glNormal3fv( TVec3(1.0f, 0.0f, 0.0f).getData() );

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[3].getData());

	glTexCoord2fv( TVec2(1.0f, 1.0f).getData() );
	glVertex3fv(mVertices[2].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[6].getData());

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[3].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[6].getData());

	glTexCoord2fv( TVec2(0.0f, 0.0f).getData() );
	glVertex3fv(mVertices[7].getData());

	// -x
	glNormal3fv( TVec3(-1.0f, 0.0f, 0.0f).getData() );

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[4].getData());

	glTexCoord2fv( TVec2(1.0f, 1.0f).getData() );
	glVertex3fv(mVertices[5].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[1].getData());

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[4].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[1].getData());

	glTexCoord2fv( TVec2(0.0f, 0.0f).getData() );
	glVertex3fv(mVertices[0].getData());

	// +y
	glNormal3fv( TVec3(0.0f, 1.0f, 0.0f).getData() );
	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[4].getData());

	glTexCoord2fv( TVec2(1.0f, 1.0f).getData() );
	glVertex3fv(mVertices[0].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[3].getData());

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[4].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[3].getData());

	glTexCoord2fv( TVec2(0.0f, 0.0f).getData() );
	glVertex3fv(mVertices[7].getData());

	// -y
	glNormal3fv( TVec3(0.0f, -1.0f, 0.0f).getData() );
	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[2].getData());

	glTexCoord2fv( TVec2(1.0f, 1.0f).getData() );
	glVertex3fv(mVertices[1].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[5].getData());

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[2].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[5].getData());

	glTexCoord2fv( TVec2(0.0f, 0.0f).getData() );
	glVertex3fv(mVertices[6].getData());

	// -z
	glNormal3fv( TVec3(0.0f, 0.0f, -1.0f).getData() );
	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[0].getData());

	glTexCoord2fv( TVec2(1.0f, 1.0f).getData() );
	glVertex3fv(mVertices[1].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[2].getData());

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[0].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[2].getData());

	glTexCoord2fv( TVec2(0.0f, 0.0f).getData() );
	glVertex3fv(mVertices[3].getData());

	// +z
	glNormal3fv( TVec3(0.0f, 0.0f, 1.0f).getData() );
	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[7].getData());

	glTexCoord2fv( TVec2(1.0f, 1.0f).getData() );
	glVertex3fv(mVertices[6].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[5].getData());

	glTexCoord2fv( TVec2(1.0f, 0.0f).getData() );
	glVertex3fv(mVertices[7].getData());

	glTexCoord2fv( TVec2(0.0f, 1.0f).getData() );
	glVertex3fv(mVertices[5].getData());

	glTexCoord2fv( TVec2(0.0f, 0.0f).getData() );
	glVertex3fv(mVertices[4].getData());

	glEnd();
}

void TCubeRenderable::buildCubeMesh(float halfSize) {

	mVertices[0] = TVec3(-halfSize, halfSize,-halfSize);
	mVertices[1] = TVec3(-halfSize,-halfSize,-halfSize);
	mVertices[2] = TVec3( halfSize,-halfSize,-halfSize);
	mVertices[3] = TVec3( halfSize, halfSize,-halfSize);

	mVertices[4] = TVec3(-halfSize, halfSize, halfSize);
	mVertices[5] = TVec3(-halfSize,-halfSize, halfSize);
	mVertices[6] = TVec3( halfSize,-halfSize, halfSize);
	mVertices[7] = TVec3( halfSize, halfSize, halfSize);

}

}